package com.masterlu.leetcode.daily.stack.simple;

import java.util.Stack;

/**
 * 1021. 删除最外层的括号
 * https://leetcode-cn.com/problems/remove-outermost-parentheses/
 */
public class S1021RemoveOuterParentheses {

    /**
     * 输入："(()())(())"
     * 输出："()()()"
     * 解释：
     * 输入字符串为 "(()())(())"，原语化分解得到 "(()())" + "(())"，
     * 删除每个部分中的最外层括号后得到 "()()" + "()" = "()()()"
     *
     * @param S 原始字符串
     * @return 处理后字符串
     */
    public static String removeOuterParentheses(String S) {

        //将内部字符串，剥离出来
        StringBuilder finalStr = new StringBuilder();
        int level = 0;
        for (char c : S.toCharArray()) {
            if (c == '(') {
                level++;
            }
            if (level > 1) {
                finalStr.append(c);
            }
            if (c == ')') {
                level--;
            }
        }


        return finalStr.toString();
    }


    public static void main(String[] args) {
        //String s = removeOuterParentheses("(()())(())");
        String s = removeOuterParentheses("(()())(())(()(()))");
        System.out.printf(s);
    }
}
